#include "virtual_intc.h"

//虚拟中断控制器的中断域
static struct irq_domain *virtual_intc_domain;

static void virtual_intc_irq_ack(struct irq_data *data)
{
	printk("function:%s line:%d\n", __FUNCTION__, __LINE__);
}

static void virtual_intc_irq_mask(struct irq_data *data)
{
	printk("function:%s line:%d\n", __FUNCTION__, __LINE__);
}

static void virtual_intc_irq_mask_ack(struct irq_data *data)
{
	printk("function:%s line:%d\n", __FUNCTION__, __LINE__);
}

static void virtual_intc_irq_unmask(struct irq_data *data)
{
	printk("function:%s line:%d\n", __FUNCTION__, __LINE__);
}

static void virtual_intc_irq_eoi(struct irq_data *data)
{
	printk("function:%s line:%d\n", __FUNCTION__, __LINE__);
}

static struct irq_chip virtual_intc_irq_chip = {
	.name = "atk_virtual_intc",
	.irq_ack = virtual_intc_irq_ack,
	.irq_mask  = virtual_intc_irq_mask,
	.irq_mask_ack = virtual_intc_irq_mask_ack,
	.irq_unmask= virtual_intc_irq_unmask,
	.irq_eoi = virtual_intc_irq_eoi,
};

//解析设备树
static int virtual_intc_irq_xlate(struct irq_domain *d, struct device_node *node,
							const u32 *intspec, unsigned int intsize,
							unsigned long *out_hwirq, unsigned int *out_type)
{
	printk("function:%s line:%d\n", __FUNCTION__, __LINE__);

	if(intsize != 2)
	{
		printk("The device tree format is incorrect\n");
		return -EINVAL;
	}
	*out_hwirq = intspec[0];
	*out_type = intspec[1] & IRQ_TYPE_SENSE_MASK;

	return 0;
}

//设置irq_desc
static int virtual_intc_irq_map(struct irq_domain *d, unsigned int virq, irq_hw_number_t hwirq)
{
	printk("function:%s line:%d\n", __FUNCTION__, __LINE__);

	//绑定中断处理函数和irq_chip
	irq_domain_set_info(d, virq, hwirq, 
						&virtual_intc_irq_chip,
						NULL, handle_level_irq,
						NULL, NULL);

	return 0;
}

//执行与 map 相反的操作
static void virtual_intc_irq_unmap(struct irq_domain *d, unsigned int virq)
{
	printk("function:%s line:%d\n", __FUNCTION__, __LINE__);
}

//解析设备树
static int virtual_intc_irq_translate(struct irq_domain *d, struct irq_fwspec *fwspec,
								unsigned long *out_hwirq, unsigned int *out_type)
{
	printk("function:%s line:%d\n", __FUNCTION__, __LINE__);

	//解析设备树
	if(fwspec->param_count != 2)
	{
		printk("The device tree format is incorrect\n");
		return -EINVAL;
	}
	*out_hwirq = fwspec->param[0];
	*out_type = fwspec->param[1] & IRQ_TYPE_SENSE_MASK;

	return 0;
}

//设置 irq_desc
static int virtual_intc_irq_alloc(struct irq_domain *d, unsigned int virq,
							unsigned int nr_irqs, void *arg)
{
	int i;
	unsigned long hwirq;
	unsigned int type;
	struct irq_fwspec *fwspec = arg;

	printk("function:%s line:%d\n", __FUNCTION__, __LINE__);


	//解析设备树
	if(fwspec->param_count != 2)
	{
		printk("The device tree format is incorrect\n");
		return -EINVAL;
	}
	hwirq = fwspec->param[0];
	type = fwspec->param[1] & IRQ_TYPE_SENSE_MASK;

	for(i=0; i<nr_irqs; i++)
	{
		irq_domain_set_info(d, virq+i, hwirq+i, 
							&virtual_intc_irq_chip,
							NULL, handle_level_irq,
							NULL, NULL);
	}

	return 0;
}

//执行与 alloc 相反的操作
static void virtual_intc_irq_free(struct irq_domain *d, unsigned int virq, unsigned int nr_irqs)
{
	printk("function:%s line:%d\n", __FUNCTION__, __LINE__);

	irq_domain_free_irqs_common(d, virq, nr_irqs);
}

static const struct irq_domain_ops virtual_intc_domain_ops = {
	.xlate = virtual_intc_irq_xlate,
	.map = virtual_intc_irq_map,
	.unmap = virtual_intc_irq_unmap,
	.alloc = virtual_intc_irq_alloc,
	.free = virtual_intc_irq_free,
	.translate = virtual_intc_irq_translate,
};

//随机产生硬件中断
static int virtual_intc_get_hwirq(void)
{
	return get_random_int() & 0x3;
}

static void virtual_intc_irq_handler(struct irq_desc *desc)
{
	/* 它的功能时分辨是哪一个hwirq, 调用对应的irq_desc[].handle_irq */
	int hwirq;
	
	struct irq_chip *chip = irq_desc_get_chip(desc);

	chained_irq_enter(chip, desc);

	/* 获取硬件中断号 */
	hwirq = virtual_intc_get_hwirq();

	/* 调用通用的中断处理函数，依次执行action中的中断事务函数 */
	generic_handle_irq(irq_find_mapping(virtual_intc_domain, hwirq));

	chained_irq_exit(chip, desc);
}

//设备和驱动匹配成功执行
static int virtual_intc_probe(struct platform_device *pdev)
{
	int irq_parent;
	int irq_base;

	//获取父中断
	irq_parent = of_irq_get(pdev->dev.of_node, 0);
	if(irq_parent <= 0)
	{
		printk("get parent failed");
		return irq_parent;
	}
	//设置父中断的中断处理函数
	irq_set_chained_handler_and_data(irq_parent, virtual_intc_irq_handler, NULL);

	//预先在内核中分配irq_desc
	irq_base = irq_alloc_descs(-1, 0, 4, numa_node_id());
	if (irq_base < 0) {
		printk("Couldn't allocate IRQ numbers\n");
		return irq_base;
	}

	//分配并初始化一个中断域
	virtual_intc_domain = irq_domain_add_legacy(pdev->dev.of_node, 4, 
												irq_base, 0, 
												&virtual_intc_domain_ops, NULL);
	if (!virtual_intc_domain)
	{
		printk("couldn't allocate irq domain (DT).\n");
		return -ENODEV;
	}

	return 0;
}

//设备或驱动卸载时执行
static int virtual_intc_remove(struct platform_device *pdev)
{
	irq_domain_remove(virtual_intc_domain);
	return 0;
}

/* 匹配列表，用于设备树和平台驱动匹配 */
static const struct of_device_id virtual_intc_of_match[] = {
	{.compatible = "atk,virtual_intc"},
	{ /* Sentinel */ }
};
//平台驱动
static struct platform_driver virtual_intc_drv = {
	.driver = {
		.name = "virtual_intc",
		.owner = THIS_MODULE,
		.pm = NULL,
		.of_match_table = virtual_intc_of_match,
	},
	.probe = virtual_intc_probe,
	.remove = virtual_intc_remove,
};

static int __init virtual_intc_drv_init(void)
{
	int result;

	printk("%s\r\n", __FUNCTION__);

	//注册平台驱动
	result = platform_driver_register(&virtual_intc_drv);
	if(result < 0)
	{
		printk("add cdev faikey\r\n");
		return result;
	}

	return 0;
}

static void __exit virtual_intc_drv_exit(void)
{
	printk("%s\r\n", __FUNCTION__);

	//注销平台驱动
	platform_driver_unregister(&virtual_intc_drv);
}

module_init(virtual_intc_drv_init);
module_exit(virtual_intc_drv_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("LF");
MODULE_DESCRIPTION("virtual_intc_drivrer");
